<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="政策文号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入政策文号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!--
      <el-form-item label="政策级别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入政策级别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行业类别" prop="industry">
        <el-input
          v-model="queryParams.industry"
          placeholder="请输入行业类别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发文部门" prop="dept">
        <el-input
          v-model="queryParams.dept"
          placeholder="请输入发文部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入来源"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题分类" prop="topic">
        <el-input
          v-model="queryParams.topic"
          placeholder="请输入主题分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省市地区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入省市地区"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期" prop="pubDate">
        <el-input
          v-model="queryParams.pubDate"
          placeholder="请输入发布日期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:policy:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:policy:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:policy:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="policyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" width="220"/>
      <el-table-column label="政策文号" align="center" prop="code" />
      <el-table-column label="政策类型" align="center" prop="type" />
      <el-table-column label="政策级别" align="center" prop="category" />
      <el-table-column label="行业类别" align="center" prop="industry" />
      <el-table-column label="发文部门" align="center" prop="dept" />
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column label="主题分类" align="center" prop="topic" />
      <el-table-column label="省市地区" align="center" prop="area" />
      <el-table-column label="发布日期" align="center" prop="pubDate" />
      <!--
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="内容文本" align="center" prop="contentTxt" />
      <el-table-column label="链接" align="center" prop="link" />
      <el-table-column label="开始时间" align="center" prop="effectiveBeginDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.effectiveBeginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="effectiveEndDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.effectiveEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:policy:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:policy:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="policyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="政策文号" prop="code">
          <el-input v-model="form.code" placeholder="请输入政策文号" />
        </el-form-item>
        <el-form-item label="政策级别" prop="category">
          <el-input v-model="form.category" placeholder="请输入政策级别" />
        </el-form-item>
        <el-form-item label="行业类别" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入行业类别" />
        </el-form-item>
        <el-form-item label="发文部门" prop="dept">
          <el-input v-model="form.dept" placeholder="请输入发文部门" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="主题分类" prop="topic">
          <el-input v-model="form.topic" placeholder="请输入主题分类" />
        </el-form-item>
        <el-form-item label="省市地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入省市地区" />
        </el-form-item>
        <el-form-item label="发布日期" prop="pubDate">
          <el-input v-model="form.pubDate" placeholder="请输入发布日期" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="内容文本" prop="contentTxt">
          <el-input v-model="form.contentTxt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="链接" prop="link">
          <el-input v-model="form.link" placeholder="请输入链接" />
        </el-form-item>
        <el-form-item label="开始时间" prop="effectiveBeginDate">
          <el-date-picker clearable
            v-model="form.effectiveBeginDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="effectiveEndDate">
          <el-date-picker clearable
            v-model="form.effectiveEndDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="${comment}" prop="param1">
          <el-input v-model="form.param1" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="param2">
          <el-input v-model="form.param2" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Policy">
import { listPolicy, getPolicy, delPolicy, addPolicy, updatePolicy } from "@/api/policy/smepolicy"

const { proxy } = getCurrentInstance()

const policyList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    type: null,
    category: null,
    industry: null,
    dept: null,
    source: null,
    topic: null,
    area: null,
    pubDate: null,
    title: null,
    content: null,
    contentTxt: null,
    link: null,
    effectiveBeginDate: null,
    effectiveEndDate: null,
    param1: null,
    param2: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询【请填写功能名称】列表 */
function getList() {
  loading.value = true
  listPolicy(queryParams.value).then(response => {
    policyList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    type: null,
    category: null,
    industry: null,
    dept: null,
    source: null,
    topic: null,
    area: null,
    pubDate: null,
    title: null,
    content: null,
    contentTxt: null,
    link: null,
    effectiveBeginDate: null,
    effectiveEndDate: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    param1: null,
    param2: null
  }
  proxy.resetForm("policyRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加【请填写功能名称】"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPolicy(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改【请填写功能名称】"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["policyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePolicy(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPolicy(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + _ids + '"的数据项？').then(function() {
    return delPolicy(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/policy/export', {
    ...queryParams.value
  }, `policy_${new Date().getTime()}.xlsx`)
}

getList()
</script>
